﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace ThangNM
{
    public partial class Quiz : System.Web.UI.Page
    {
        public List<QuizEO> lstQuiz
        {
            get { return (List<QuizEO>)ViewState["lstQuiz"]; }
            set { ViewState["lstQuiz"] = value; }
        }

        public Int32 iCurrentID
        {
            get { return (Int32)ViewState["iCurrentID"]; }
            set { ViewState["iCurrentID"] = value; }
        }

        public DateTime tStart
        {
            get { return (DateTime)ViewState["tStart"]; }
            set { ViewState["tStart"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MsgUC1.setClearMsg();
                if (!IsPostBack)
                {
                    SEOEO _SEOEO = new SEOEO();
                    _SEOEO = (new CategoryDAO()).SelectBy_sQueryString_sUrl(null, OtherEx.RemoveDomainInUrl(Request.RawUrl));
                    Commons.setSEO_ON_PAGE(Page, _SEOEO, this.GetType());
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            try
            {
                QuizExEO _QuizExEO = new QuizExEO();
                object temp = JsonEx.Json2Object(lstQuiz[iCurrentID].sQuizEx, _QuizExEO.GetType());
                if (temp != null)
                {
                    hfPK_iQuizID.Value = Convert.ToString(lstQuiz[iCurrentID].PK_iQuizID);
                    lblsConten.Text = (iCurrentID + 1).ToString() + "/" + lstQuiz.Count + ". " + lstQuiz[iCurrentID].sContent;
                    _QuizExEO = (QuizExEO)temp;
                    cblChoose.DataSource = _QuizExEO.lstChoose;
                    cblChoose.DataValueField = "PK_iChooseID";
                    cblChoose.DataTextField = "sAnswer";
                    cblChoose.DataBind();
                    foreach (ListItem item in cblChoose.Items)
                    {
                        if (Commons.CheckExistInBinary(lstQuiz[iCurrentID].iLevel, Convert.ToInt16("1".PadRight(Convert.ToInt16(item.Value), '0'), 2), false))
                            item.Selected = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            btnCheck.Visible = true;
            MsgUC1.setClearMsg();
            lstQuiz = new List<QuizEO>();
            DataTable dtQuiz = (new QuizDAO()).SelectBy_iLevel_iQuantity(Convert.ToInt16(ddliLevel.SelectedValue), Convert.ToInt16(ddliQuantity.SelectedValue));
            lstQuiz = (List<QuizEO>)ConvertEx.ConvertDataTableToListObject<QuizEO>(dtQuiz, lstQuiz.GetType());
            if (lstQuiz == null)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Hiện tại không có câu hỏi nào để thi. Vui lòng thử với các lựa chọn khác!");
                return;
            }
            foreach (var item in lstQuiz)
            {
                item.iLevel = 0;
                item.iStatus = 0;
            }
            mtvMain.SetActiveView(vQuiz);
            tClock.Enabled = true;
            iCurrentID = 0;
            tStart = DateTime.Now;
            BindData();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            lstQuiz = null;
            mtvMain.SetActiveView(vOption);
            tClock.Enabled = false;
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            MsgUC1.setClearMsg();
            checkTrue();
            int iTrue = 0;
            foreach (var item in lstQuiz)
            {
                if (item.iStatus == 1)
                    iTrue = iTrue + 1;
            }
            MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Bạn đã trả lời đúng " + iTrue + "/" + lstQuiz.Count);
            tClock.Enabled = false;
            btnCheck.Visible = false;
        }

        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            checkTrue();
            iCurrentID = iCurrentID - 1;
            if (iCurrentID < 0)
            {
                iCurrentID = lstQuiz.Count - 1;
            }
            BindData();
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            checkTrue();
            iCurrentID = iCurrentID + 1;
            if (iCurrentID >= lstQuiz.Count)
            {
                iCurrentID = 0;
            }
            BindData();
        }

        protected void tClock_Tick(object sender, EventArgs e)
        {
            var ts = TimeSpan.FromSeconds((DateTime.Now - tStart).TotalSeconds - 1);
            lblClock.Text = String.Format("Bạn đã sử dụng {0} giờ, {1} phút, {2} giây.", ts.Hours, ts.Minutes, ts.Seconds);
        }

        protected void checkTrue()
        {
            String sBinary = "";
            foreach (ListItem item in cblChoose.Items)
            {
                if (item.Selected)
                    sBinary = "1" + sBinary;
                else
                    sBinary = "0" + sBinary;
            }
            lstQuiz[iCurrentID].iLevel = Convert.ToInt16(sBinary, 2);
            if (Commons.CheckExistInBinary(lstQuiz[iCurrentID].iChooseTrue, lstQuiz[iCurrentID].iLevel, true))
                lstQuiz[iCurrentID].iStatus = 1;
            else
                lstQuiz[iCurrentID].iStatus = 0;
        }

    }
}